package com.htt.app.bienbaogiaothong.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.htt.app.bienbaogiaothong.DetailsActivity;
import com.htt.app.bienbaogiaothong.R;
import com.htt.app.bienbaogiaothong.objects.Details;
import com.htt.app.bienbaogiaothong.task.LoadImageAssets;
import com.htt.app.bienbaogiaothong.utils.Unicode;

public class ListAdapter extends BaseAdapter{
	private Context context;
	private List<Details> data = null;
	private ArrayList<Details> arrayList = null;
	private LayoutInflater inflater  = null;
	private AssetManager assets = null;
	
	public ListAdapter(Context context, List<Details> list) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.data = list;
		arrayList = new ArrayList<Details>();
		arrayList.addAll(data);
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		assets = context.getAssets();
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View v = convertView;
		ViewHolder holder;
		
		if(convertView == null) {
			v = inflater.inflate(R.layout.list_item, null);
			holder = new ViewHolder();
			holder.imageView = (ImageView) v.findViewById(R.id.list_item_image);
			holder.textView = (TextView) v.findViewById(R.id.list_item_name);
			
			v.setTag(holder);
		} else {
			holder = (ViewHolder) v.getTag();
		}
		
		holder.textView.setText(data.get(position).getName());
		LoadImageAssets loadImage = new LoadImageAssets(assets, holder.imageView,
				data.get(position).getImage());
		loadImage.execute();
		
		v.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(context, DetailsActivity.class);
				intent.putExtra("image", data.get(position).getImage());
				intent.putExtra("name", data.get(position).getName());
				intent.putExtra("detail", data.get(position).getDetail());
				intent.putExtra("number", data.get(position).getNumber());
				context.startActivity(intent);
			}
		});
		
		return v;
	}
	
	private static class ViewHolder {
		ImageView imageView;
		TextView textView;
	}
	
	// Filter Class
		public void filter(String charText) {
			charText = charText.toLowerCase(Locale.getDefault());
			charText = new Unicode().ConvertString(charText);
			data.clear();
			if (charText.length() == 0) {
				data.addAll(arrayList);
			} 
			else 
			{
				for (Details detail : arrayList) 
				{
					if ((detail.getName_el() != null && detail.getName_el().toLowerCase(Locale.getDefault()).contains(charText))
							|| (detail.getNumber() != null && detail.getNumber().toLowerCase(Locale.getDefault()).contains(charText)) ) 
					{
						data.add(detail);
					}
				}
			}
			notifyDataSetChanged();
		}
}
